home *** CD-ROM | disk | FTP | other *** search
/ BBS Toolkit / BBS Toolkit.iso / doors_1 / gbs2bbs.zip / GBS2BBS.C < prev    next >
C/C++ Source or Header  |  1993-01-04  |  15KB  |  388 lines

  1. /* Version 1.1 modified on 21-Oct-1987 by George A. Stanislav    */
  2. /*                                */
  3. /*    Original by Wynn Wagner III                */
  4.  
  5. #ifndef LINT_ARGS
  6. #define LINT_ARGS
  7. #endif
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <io.h>
  12. #include <ctype.h>
  13. #include "legible.h"
  14.  
  15. /*--------------------------------------------------------------------------*/
  16. /* External declarations                                                    */
  17. /*--------------------------------------------------------------------------*/
  18. extern int  errno;
  19.  
  20. /*--------------------------------------------------------------------------*/
  21. /* Global declarations                                                      */
  22. /*--------------------------------------------------------------------------*/
  23. FILE       *source         = NULL;
  24. FILE       *dest           = NULL;
  25. int         ansi_digit[4];
  26.  
  27.  
  28.  
  29. /*--------------------------------------------------------------------------*/
  30. /* USAGE                                                                    */
  31. /*--------------------------------------------------------------------------*/
  32. void usage()
  33.    begin
  34.       cputs("Converts GBS files into BBS files using Opus optimized video controls.\r\n\n\USAGE: Gbs2Bbs <filename>\r\n\n");
  35.       exit(1);
  36.    end /* usage */
  37.  
  38.  
  39.  
  40. /*--------------------------------------------------------------------------*/
  41. /* CAN'T (do something)                                                     */
  42. /*--------------------------------------------------------------------------*/
  43. void cant( do_what, to_whom )
  44.    char *do_what;
  45.    char *to_whom;
  46.    begin
  47.       char s[100];
  48.  
  49.       sprintf(s, "Can't %s %s", do_what, to_whom );
  50.       perror(s);
  51.  
  52.       if (dest)   fclose(dest);
  53.       if (source) fclose(source);
  54.  
  55.       exit(1);
  56.  
  57.    end /* Can't */
  58.  
  59.  
  60.  
  61. /*--------------------------------------------------------------------------*/
  62. /* MAKE FILE NAME                                                           */
  63. /* Build a new file name (node), but use a different tag.                   */
  64. /*--------------------------------------------------------------------------*/
  65. void make_file_name( node, tag )
  66.    char *node;
  67.    char *tag;
  68.    begin
  69.       register char *sptr;
  70.  
  71.       if ( !(sptr=strchr(node,'.')) )  strcat(node,tag);
  72.       else                             strcpy(sptr,tag);
  73.  
  74.    end /* make file name */
  75.  
  76.  
  77. /*--------------------------------------------------------------------------*/
  78. /* COMMAND                                                                  */
  79. /*--------------------------------------------------------------------------*/
  80. void command(which)
  81.    byte which;
  82.    begin
  83.       int   i;
  84.  
  85.       if (ansi_digit[0]>0)
  86.          begin
  87.             for(i=0; i<ansi_digit[0]; i++)
  88.                begin
  89.                   putc( 0x16, dest );
  90.                   putc( which,dest );
  91.                end
  92.             return;
  93.          end
  94.       putc(0x16, dest);
  95.       putc(which,dest);
  96.    end /* command */
  97.  
  98.  
  99.  
  100. /*--------------------------------------------------------------------------*/
  101. /* COLOR                                                                    */
  102. /*--------------------------------------------------------------------------*/
  103. #define BACKGROUND(x) attribute=(attribute&0x8f)+(x)
  104. #define FOREGROUND(x) attribute=(attribute&0xf8)+(x)
  105.  
  106. void color()
  107.    begin
  108.       int i;
  109.       int j;
  110.       int blink = 0;
  111.  
  112.       register byte attribute = 0x07;
  113.  
  114.       for(attribute=i=0; i<4; i++)
  115.          switch(ansi_digit[i])
  116.             begin
  117.                case  0: attribute     = 0x07;                  break;
  118.                case  1: if (!(attribute & 0x0f))
  119.                attribute |= 0x0f;
  120.             else
  121.                attribute    |= 0x08;               break;
  122.                case  4: FOREGROUND(0x01);                      break;
  123.                case  5: blink         = 0xffff;               break;
  124.                case  7: attribute     = 0x70;                  break;
  125.  
  126.                case 30: FOREGROUND(0x00);                      break;
  127.                case 31: FOREGROUND(0x04);                      break;
  128.                case 32: FOREGROUND(0x02);                      break;
  129.                case 33: FOREGROUND(0x06);                      break;
  130.                case 34: FOREGROUND(0x01);                      break;
  131.                case 35: FOREGROUND(0x05);                      break;
  132.                case 36: FOREGROUND(0x03);                      break;
  133.                case 37: FOREGROUND(0x07);                      break;
  134.  
  135.                case 40: BACKGROUND(0x00);                      break;
  136.                case 41: BACKGROUND(0x40);                      break;
  137.                case 42: BACKGROUND(0x20);                      break;
  138.                case 43: BACKGROUND(0x60);                      break;
  139.                case 44: BACKGROUND(0x10);                      break;
  140.                case 45: BACKGROUND(0x50);                      break;
  141.                case 46: BACKGROUND(0x30);                      break;
  142.                case 47: BACKGROUND(0x70);                      break;
  143.             end
  144.  
  145.       if ((!blink) || attribute)
  146.     begin
  147.        putc( 0x16,       dest );
  148.        putc( 0x01,       dest );
  149.        putc( 0x10,       dest );
  150.        attribute    |=   0x80  ;
  151.        putc( attribute,  dest );
  152.     end
  153.  
  154.       if (blink)
  155.     begin
  156.        ansi_digit[0] = 0xffff;
  157.        command(0x02);
  158.     end
  159.  
  160.    end
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167. /*--------------------------------------------------------------------------*/
  168. /* MAIN                                                                     */
  169. /*--------------------------------------------------------------------------*/
  170. main(argc,argv)
  171.    int   argc;
  172.    char *argv[];
  173.    begin
  174.       register char *sptr;
  175.       register int   i;
  176.  
  177.       char           old_name[80];
  178.       char           new_name[80];
  179.       char           buffer[260];
  180.  
  181.       int            line;
  182.       int            j;
  183.       int            handle;
  184.  
  185.  
  186.       /*--------------------------------------------------------------------*/
  187.       /* Say Howdy                                                          */
  188.       /*--------------------------------------------------------------------*/
  189.       cputs("Gbs2Bbs [1.1]\r\n");
  190.       if (argc<2) usage();
  191.  
  192.  
  193.       /*--------------------------------------------------------------------*/
  194.       /* Turn the existing file into a BAK                                  */
  195.       /*--------------------------------------------------------------------*/
  196.       strcpy( new_name, argv[1] );
  197.       if (!strchr(new_name,'.')) make_file_name( new_name, ".Bbs" );
  198.  
  199.       strcpy( old_name, new_name );
  200.  
  201.       make_file_name( old_name, ".Bak" );
  202.       unlink( old_name );
  203.  
  204.       if (rename(new_name,old_name)) cant("rename",old_name);
  205.  
  206.  
  207.       /*--------------------------------------------------------------------*/
  208.       /* Setup for the conversion                                           */
  209.       /*--------------------------------------------------------------------*/
  210.       line     =
  211.       errno    = 0;
  212.  
  213.       make_file_name( new_name, ".Bbs" );
  214.  
  215.       source   = fopen( old_name, "rb" );
  216.       if (errno) cant("open",old_name);
  217.  
  218.       dest     = fopen( new_name, "wb" );
  219.       if (errno) cant("create",new_name);
  220.  
  221.  
  222.       /*--------------------------------------------------------------------*/
  223.       /* The conversion                                                     */
  224.       /*--------------------------------------------------------------------*/
  225.       while(!feof(source))
  226.          begin
  227.             sptr     = buffer;
  228.             *sptr    = '\0';
  229.             fgets(sptr,256,source);
  230.             line++;
  231.  
  232.             while(1)
  233.                begin
  234.                   switch( *sptr )
  235.                       begin
  236.                         default:    /*--------------------------------------*/
  237.                                     /* Check on replicating characters      */
  238.                                     /* (Needs to go down to .ASM badly!)    */
  239.                                     /*--------------------------------------*/
  240.                                     for(i=1; sptr[i]==sptr[0]; i++)
  241.                                        ;
  242.                                     if (i>=5)
  243.                                        begin
  244.                       if (i==26)
  245.                          begin
  246.                         putc( *sptr,dest );
  247.                         i--;
  248.                          end
  249.                                           putc( 0x19, dest ); /* ctrl-Y */
  250.                                           putc( *sptr,dest ); /* value  */
  251.                                           putc( i,    dest ); /* factor */
  252.                                           sptr += i;
  253.                                        end
  254.                                     else
  255.                                        begin
  256.                                           putc(*sptr,dest);
  257.                                           sptr++;
  258.                                        end
  259.  
  260.                                     break;
  261.  
  262.                         case 0x1a:  /*--------------------------------------*/
  263.                                     /* End of the file                      */
  264.                                     /*--------------------------------------*/
  265.                                     goto done;
  266.  
  267.                           case 0:     /*--------------------------------------*/
  268.                                     /* End of this line                     */
  269.                                     /*--------------------------------------*/
  270.                                     goto nxt;
  271.  
  272.                         case 0x1b:  /*--------------------------------------*/
  273.                                     /* Escape (ANSI)                        */
  274.                                     /*--------------------------------------*/
  275.                                     for(i=0; i<4; i++)
  276.                                        ansi_digit[i] = 0xffff;
  277.  
  278.                                     i  = 0;
  279.  
  280.                                     sptr++;
  281.                                     if (*sptr!='[') break;
  282.  
  283.                                     sptr++;
  284.  
  285.                                     for(;;) switch (*sptr)
  286.                                        begin
  287.                                           case '0' :
  288.                                           case '1' :
  289.                                           case '2' :
  290.                                           case '3' :
  291.                                           case '4' :
  292.                                           case '5' :
  293.                                           case '6' :
  294.                                           case '7' :
  295.                                           case '8' :
  296.                                           case '9' :
  297.                                                      if (i>4)
  298.                                                         begin
  299.                                                            cprintf("Too complicated, line %d",line);
  300.                                                            exit(1);
  301.                                                         end
  302.                                                      ansi_digit[i] = atoi(sptr);
  303.                                                      do sptr++; while(isdigit(*sptr));
  304.                                                      i++;
  305.                                                      break;
  306.  
  307.                                           case ';' : sptr++;
  308.                                                      break;
  309.  
  310.                                           case 'k' : /* erase EOL           */
  311.                                                      command(0x07);
  312.                                                      sptr++;
  313.                                                      goto ansi_done;
  314.  
  315.                                           case 'A' : /* go up               */
  316.                                                      command(0x03);
  317.                                                      sptr++;
  318.                                                      goto ansi_done;
  319.  
  320.                                           case 'B' : /* go down             */
  321.                                                      command(0x04);
  322.                                                      sptr++;
  323.                                                      goto ansi_done;
  324.  
  325.                                           case 'C' : /* go right            */
  326.                                                      command(0x06);
  327.                                                      sptr++;
  328.                                                      goto ansi_done;
  329.  
  330.                                           case 'D' : /* go left             */
  331.                                                      command(0x05);
  332.                                                      sptr++;
  333.                                                      goto ansi_done;
  334.  
  335.                                           case 'J' : /* clear EOS           */
  336.                                                      putc( 0x0c, dest );
  337.                                                      sptr++;
  338.                                                      goto ansi_done;
  339.  
  340.                                           case 'm' : /* color               */
  341.                                                      color();
  342.                                                      sptr++;
  343.                                                      goto ansi_done;
  344.  
  345.                                           case 'f' : /* set cursor position */
  346.                                           case 'H' : /* set cursor position */
  347.                                                      if (ansi_digit[0]<0)
  348.                                                         ansi_digit[0] = 0;
  349.                                                      if (ansi_digit[1]<0)
  350.                                                         ansi_digit[1] = 0;
  351.                                                      putc(0x16, dest);
  352.                                                      putc(0x08, dest);
  353.                                                      putc(ansi_digit[0],dest);
  354.                                                      putc(ansi_digit[1],dest);
  355.                                                      sptr++;
  356.                                                      goto ansi_done;
  357.  
  358.                                           default  : /* we can't do this    */
  359.                                                      cprintf("Unknown ANSI command, line %d\n",line);
  360.                                                      exit(1);
  361.                                                      
  362.  
  363.                                        end /* switch */
  364.  
  365.                                     break;
  366.  
  367.                       end /* switch */
  368. ansi_done:        ;
  369.                   
  370.  
  371.                end
  372. nxt:        ;
  373.       
  374.          end
  375.  
  376. done:
  377.       fclose(source);
  378.       fclose(dest);
  379.  
  380.    end /* main */
  381.  
  382.  
  383.  
  384.  
  385.  
  386. /* END OF FILE: make file name */
  387.  
  388.